package info.nixbear.tgcms.admin.client.activity;

import info.nixbear.tgcms.admin.client.place.RedirectsPlace;
import info.nixbear.tgcms.admin.client.view.RedirectsView;
import info.nixbear.tgcms.admin.shared.request.AdminRequestFactory;
import info.nixbear.tgcms.admin.shared.request.RedirectProxy;

import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.shared.Receiver;

public class RedirectsActivity extends AbstractActivity {

	private RedirectsView view;
	private PlaceController placeController;
	private AdminRequestFactory requestFactory;
	
	@Inject
	public RedirectsActivity(
			RedirectsView view, 
			PlaceController placeController,
			AdminRequestFactory requestFactory) 
	{
		this.view = view;
		this.placeController = placeController;
		this.requestFactory = requestFactory;
	}

	public RedirectsActivity withPlace(RedirectsPlace place) {
	    return this;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setRedirectsActivity(this);
		panel.setWidget(view.asWidget());
		requestFactory.redirectRequest().listAll().fire(
				new Receiver<List<RedirectProxy>>() {
					@Override
					public void onSuccess(List<RedirectProxy> redirects) {
						view.setRedirects(redirects);
					}
				});
	}
	
	public void goTo(Place place) {
		placeController.goTo(place);
	}

	@Override
	public void onStop() {
		view.setRedirectsActivity(null);
	}

	

}
